package com.kzd.btgame.main.money

import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityMoneyTaskBossBinding
import com.kzd.btgame.jetpack.vm.MoneyViewModel
import com.kzd.btgame.main.ProblemDetailsAction
import com.kzd.btgame.main.money.adapter.MoneyBossInviteAdapter
import com.kzd.btgame.main.money.adapter.MoneyBossSharedAdapter
import com.kzd.btgame.main.toProblemDetailsActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MoneyTaskBossActivity : OwnerViewBindingActivity<ActivityMoneyTaskBossBinding>() {

    private val mMoneyBossSharedAdapter = MoneyBossSharedAdapter()

    private val mMoneyBossInviteAdapter = MoneyBossInviteAdapter()

    private val mViewModel: MoneyViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMoneyTaskBossBinding
        get() = ActivityMoneyTaskBossBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMoneyTaskBossBar
            barColor = R.color.colorMain
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        mViewBinding.rvMoneyTaskBossSharedBody.adapter = mMoneyBossSharedAdapter.apply {
            setOnItemChildClickListener { _, view, position ->
                val itemData = getItem(position)
                if (view.id == R.id.tvMoneyBossSharedSubmit) {
                    mViewModel.userShare(
                        activity = this@MoneyTaskBossActivity,
                        url = itemData.share,
                        title = itemData.share_title,
                        desc = itemData.share_content
                    )
                }
            }
        }

        mViewBinding.rvMoneyTaskBossInviteBody.adapter = mMoneyBossInviteAdapter

        mViewBinding.tvMoneyTaskBossDetails.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.AWARD)
        }
    }

    override fun initData() {
        mViewModel.moneyBossShared()
        mViewModel.moneyBossInvite()
    }

    override fun initViewMode() {
        mViewModel.moneyBossSharedLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.isNullOrEmpty()) {
                mViewBinding.rvMoneyTaskBossSharedBody.visibility = View.GONE
                mViewBinding.rvMoneyTaskBossBackImage.visibility = View.VISIBLE
            } else {
                mViewBinding.rvMoneyTaskBossSharedBody.visibility = View.VISIBLE
                mViewBinding.rvMoneyTaskBossBackImage.visibility = View.GONE
                mMoneyBossSharedAdapter.setNewInstance(data)
            }
        })
        mViewModel.moneyBossInviteLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyBossInviteAdapter.setNewInstance(data)
        })
    }
}